<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
</head>
<body>
<div>
    1234
</div>
<script>
    var websocket = null;
    if('WebSocket' in window) {
        websocket = new WebSocket('ws://127.0.0.1:8080/webSocket')
    }else{
        alert('改浏览器不支持websocket！')
    }

    websocket.onopen = function (ev) {
        console.log('建立连接')
        websocket.send('建立连接后的第一个消息')
    }

    websocket.onclose = function (ev) {
        console.log('链接关闭')
    }

    websocket.onmessage = function (ev) {
        console.log('收到消息：'+ev.data)
        // 根据提示做。。。
    }

    websocket.onerror = function (ev) {
        alert('websocket通信发生错误！')
    }

    window.onbeforeunload = function (ev) {
        websocket.close()
    }
</script>
</body>
</html>